package enumdemo;

public enum TestEnum { //将class改成enum表示枚举类  继承于Enum类
    Red("RED",1),
    WHITE("WHITE",2),
    GREEN("GREEN",3);  //枚举对象

    public int ordinal;
    public String color;

    private TestEnum(String color,int ordinal) {
        this.ordinal = ordinal;
        this.color = color;
    }


    public static void main(String[] args) {
         TestEnum testEnum = TestEnum.Red;
         switch (testEnum) {
             case Red:
                 System.out.println("红色");
                 break;
             case GREEN:
                 System.out.println("绿色");
                 break;
             case WHITE:
                 System.out.println("红色");
                 break;
             default:break;
         }


          TestEnum[] testEnums= TestEnum.values();//转成枚举数组  由编译器添加的一个静态方法
         for(int i = 0 ; i < testEnums.length; ++i) {
             System.out.println(testEnums[i] + " " +testEnums[i].ordinal());  //ordinal方法是返回每个枚举对象对应的值
         }

         TestEnum t = TestEnum.valueOf("GREEN"); //将字符串转换成枚举类型
         System.out.println(t);

    }

}
